//
//  ZXMessageCell.m
//  ZXDNLLTest
//
//  Created by mxsm on 16/5/18.
//  Copyright © 2016年 mxsm. All rights reserved.
//

#import "ZXMessageCell.h"
#import "ZXChatHelper.h"
@implementation ZXMessageCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.messageBackgroundImageView];
        [self addSubview:self.avatarImageView];
        [self addSubview:self.labManager];
        [self addSubview:self.labName];
        [self addSubview:self.labTime];
    }
    
    return  self;
}


-(void)layoutSubviews
{
    
    /**
     *  聊天的具体界面，只要考虑这两种类型，自己的，别人的。
     */
    [super layoutSubviews];

    
    self.labName.text=_messageModel.from.NickName;
    self.labTime.text=_messageModel.dateString;
    CGSize size=[TGTools boundingRectWithString:_messageModel.from.NickName withSize:CGSizeMake(SCREEN_WIDTH, 30) anfont:13];
    if (_messageModel.ownerTyper == ZXMessageOwnerTypeSelf) {
        // 屏幕宽 - 10 - 头像宽
        [self.avatarImageView setOrigin:CGPointMake(self.frameWidth - 10 - self.avatarImageView.frameWidth, 10)];
        [self.labTime setFrame:CGRectMake(self.frameWidth - 20 - self.avatarImageView.frameWidth-43, 10, 41, 20)];
        self.labName.textAlignment=NSTextAlignmentCenter;
        [self.labName setFrame:CGRectMake(self.frameWidth-self.labTime.frame.size.width - 20 - self.avatarImageView.frameWidth-size.width-10, 5, size.width+10, 30)];
        if (_messageModel.isAdmin||_messageModel.from.MasterLottery.length>0) {
            if (_messageModel.isAdmin) {
                self.labManager.text=@"管理员";
                _labManager.backgroundColor=NAVCOLOR;
            }else{
                self.labManager.text=_messageModel.from.MasterLottery;
                _labManager.backgroundColor=DEFAULTORANGECOLOR;
            }
            float width=[TGTools boundingRectWithString:self.labManager.text withWidth:SCREEN_WIDTH anfont:self.labManager.font];
            self.labManager.hidden=NO;
            self.labManager.frame=CGRectMake(self.frameWidth - 20 - self.avatarImageView.frameWidth-self.labName.frameWidth-width-8-self.labTime.frameWidth, 12, width+8, 17);
        }else{
            self.labManager.hidden=YES;
        }
    }
    else if (_messageModel.ownerTyper == ZXMessageOwnerTypeOther) {
        [self.avatarImageView setOrigin:CGPointMake(10, 10)];
        self.labName.textAlignment=NSTextAlignmentCenter;
        if (_messageModel.isAdmin||_messageModel.from.MasterLottery.length>0) {
            self.labManager.hidden=NO;
            if (_messageModel.isAdmin) {
                self.labManager.text=@"管理员";
                _labManager.backgroundColor=NAVCOLOR;
            }else{
                self.labManager.text=_messageModel.from.MasterLottery;
                _labManager.backgroundColor=DEFAULTORANGECOLOR;
            }
            float width=[TGTools boundingRectWithString:self.labManager.text withWidth:SCREEN_WIDTH anfont:self.labManager.font];
            self.labManager.frame=CGRectMake(self.avatarImageView.frameWidth+20+4, 12, width+8, 17);
            [self.labName setFrame:CGRectMake(self.avatarImageView.frameWidth+24+self.labManager.frame.size.width, 5, size.width+5, 30)];
            [self.labTime setFrame:CGRectMake(self.avatarImageView.frameWidth+24+self.labManager.frame.size.width    +self.labName.frame.size.width, 10, 41, 20)];
        }else{
            self.labManager.hidden=YES;
            [self.labName setFrame:CGRectMake(self.avatarImageView.frameWidth+20-3, 5, size.width+5, 30)];
            [self.labTime setFrame:CGRectMake(self.avatarImageView.frameWidth+20 +self.labName.frame.size.width, 10, 41, 20)];
        }
    }
}

-(void)setMessageModel:(ZXMessageModel *)messageModel
{
    
    _messageModel = messageModel;
    switch (_messageModel.ownerTyper) {
        case ZXMessageOwnerTypeSelf:
            [self.avatarImageView setHidden:NO];
            [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:messageModel.from.Avater]];
            [self.messageBackgroundImageView setHidden:NO];
            self.messageBackgroundImageView.image = [[UIImage imageNamed:@"message_sender_background_normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 20, 15, 20) resizingMode:UIImageResizingModeStretch];

            // 设置高亮图片
            self.messageBackgroundImageView.highlightedImage = [[UIImage imageNamed:@"message_sender_background_normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 20, 15, 20) resizingMode:UIImageResizingModeStretch];
            break;
         
        case ZXMessageOwnerTypeOther:
            [self.avatarImageView setHidden:NO];
            [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:messageModel.from.Avater]];
            [self.messageBackgroundImageView setHidden:NO];
            [self.messageBackgroundImageView setImage:[[UIImage imageNamed:@"message_receiver_background_normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 20, 15, 20) resizingMode:UIImageResizingModeStretch]];
            self.messageBackgroundImageView.highlightedImage = [[UIImage imageNamed:@"message_receiver_background_highlight"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 20, 15, 20) resizingMode:UIImageResizingModeStretch];
            break;
            
        case ZXMessageOwnerTypeSystem:
            
//            [self.avatarImageView setHidden:YES];
//            [self.messageBackgroundImageView setHidden:YES];
            
            break;
            
        default:
            
            break;
    
    }
}


#pragma mark - Interactions
- (void)longPress:(UILongPressGestureRecognizer *)recognizer
{
    if ((recognizer.state != UIGestureRecognizerStateBegan)) {
        return;
    }
    [self becomeFirstResponder];
    if (self.longPressBlock) {
        self.longPressBlock(recognizer.view,_messageModel);
    }
}

- (BOOL)canBecomeFirstResponder{
    return YES;
}



/**
 * avatarImageView 头像
 */

-(UIImageView *)avatarImageView
{
    if (_avatarImageView == nil) {
        float imageWidth = 40;
        _avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, imageWidth, imageWidth)];
        _avatarImageView.layer.masksToBounds=YES;
        _avatarImageView.layer.cornerRadius=5.0;
        [_avatarImageView setHidden:YES];
    }
    return _avatarImageView;
}
/**
 *  聊天背景图
 */
- (UIImageView *) messageBackgroundImageView
{
    if (_messageBackgroundImageView == nil) {
        _messageBackgroundImageView = [[UIImageView alloc] init];
        [_messageBackgroundImageView setHidden:YES];
        _messageBackgroundImageView.userInteractionEnabled=YES;
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        longPressGesture.minimumPressDuration=1.0;
        [_messageBackgroundImageView addGestureRecognizer:longPressGesture];
    }
    return _messageBackgroundImageView;
}

- (QXLable *)labName{
    if (!_labName) {
        _labName=[[QXLable alloc]init];
        _labName.font=[UIFont systemFontOfSize:12];
    }
    return _labName;
}

- (QXLable *)labManager{
    if (!_labManager) {
        _labManager=[[QXLable alloc]init];
        _labManager.backgroundColor=NAVCOLOR;
        _labManager.font=[UIFont systemFontOfSize:9];
        _labManager.textAlignment=NSTextAlignmentCenter;
        _labManager.text=@"管理员";
        _labManager.textColor=[UIColor whiteColor];
        _labManager.layer.masksToBounds=YES;
        _labManager.layer.cornerRadius=3.0;
    }
    return _labManager;
}

- (QXLable *)labTime{
    if (!_labTime) {
        _labTime=[[QXLable alloc]init];
        _labTime.backgroundColor=DEFAULTLINECOLOR;
        _labTime.font=[UIFont systemFontOfSize:12];
        _labTime.textAlignment=NSTextAlignmentCenter;
        _labTime.layer.masksToBounds=YES;
        _labTime.layer.cornerRadius=10.0;
    }
    return _labTime;
}

@end
